package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Playlist;
import com.softeem.music_sys.service.PlaylistService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 歌单控制器
 */
@Tag(name = "歌单接口", description = "歌单相关操作:创建、查询、修改、删除等")
@RestController
@RequestMapping("/playlist")
public class PlaylistController {
    
    @Autowired
    private PlaylistService playlistService;

    @Operation(summary = "创建歌单", description = "创建新歌单")
    @PostMapping
    public Result add(@RequestBody Playlist playlist) {
        return playlistService.addPlaylist(playlist);
    }

    @Operation(summary = "获取歌单信息", description = "根据ID获取歌单详细信息")
    @GetMapping("/{id}")
    public Result getPlaylistById(@PathVariable Integer id) {
        return playlistService.getPlaylistById(id);
    }

    @Operation(summary = "更新歌单信息", description = "更新歌单信息")
    @PutMapping
    public Result update(@RequestBody Playlist playlist) {
        return playlistService.updatePlaylist(playlist);
    }

    @Operation(summary = "删除歌单", description = "根据ID删除歌单")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return playlistService.deletePlaylist(id);
    }

    @Operation(summary = "获取歌单列表", description = "分页获取歌单列表")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer limit) {
        Page<Playlist> resultPage = playlistService.getPlaylistList(page, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "搜索歌单", description = "根据歌单名搜索")
    @GetMapping("/search")
    public Result search(@RequestParam String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit) {
        Page<Playlist> resultPage = playlistService.searchPlaylists(name, page, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "获取用户的歌单", description = "根据用户ID获取其所有歌单")
    @GetMapping("/user/{userId}")
    public Result getByUser(@PathVariable Integer userId,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit) {
        Page<Playlist> resultPage = playlistService.getPlaylistsByUser(userId, page, limit);
        return Result.success().setData(resultPage);
    }

    @Operation(summary = "获取最新歌单", description = "根据创建时间获取最新歌单")
    @GetMapping("/new")
    public Result getNewPlaylist(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit) {
        Page<Playlist> resultPage = playlistService.getNewPlaylists(page, limit);
        return Result.success().setData(resultPage);
    }
}